#include "wlan.h"

int get_ip_address(char *interface, char *ip_address) 
{
    struct ifaddrs *ifaddr, *ifa;
    int family, s;
    char host[NI_MAXHOST];

    if (getifaddrs(&ifaddr) == -1) 
    {
        perror("getifaddrs");
        return -1;
    }

    for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) 
    {
        if (ifa->ifa_addr == NULL)
            continue;

        family = ifa->ifa_addr->sa_family;

        if (strcmp(ifa->ifa_name, interface) == 0 && family == AF_INET) 
        {
            s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
            if (s != 0) {
                printf("getnameinfo() failed: %s", gai_strerror(s));
                return -1;
            }
            strcpy(ip_address, host);
            break;
        }
    }

    freeifaddrs(ifaddr);
    return 0;
}



int get_mac_address(char *interface, char *mac_address) 
{
    struct ifreq ifr;
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0) {
        perror("socket");
        return -1;
    }

    strncpy(ifr.ifr_name, interface, IFNAMSIZ);
    if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) < 0) 
    {
        perror("ioctl");
        close(sockfd);
        return -1;
    }

    close(sockfd);

    // sprintf(mac_address, "%02x%02x%02x%02x%02x%02x",
    //         (unsigned char)ifr.ifr_hwaddr.sa_data[0],
    //         (unsigned char)ifr.ifr_hwaddr.sa_data[1],
    //         (unsigned char)ifr.ifr_hwaddr.sa_data[2],
    //         (unsigned char)ifr.ifr_hwaddr.sa_data[3],
    //         (unsigned char)ifr.ifr_hwaddr.sa_data[4],
    //         (unsigned char)ifr.ifr_hwaddr.sa_data[5]);
memcpy(mac_address, ifr.ifr_hwaddr.sa_data, 6);


    

    return 0;
}

